<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/7
 * Time: 下午5:12
 */

namespace app\api\controller;


use think\Controller;

class ApiPrint extends Controller
{
    public function wsyprint($param, $keys)
    {
        $url = "http://client.notify.semoor.cn/notify.php";  //新地址

        $data['device_id'] = $param['device_id'];//"269683"; //设备编号
        $sparam['paytime'] = $param['paytime'];
        $sparam['company'] = $param['merchant'];//商户名称
        $sparam['no'] = $param['out_trade_no'];//订单号
        $sparam['staff'] = $param['staff'];//服务员
        $sparam['total'] = $param['total_fee'];//支付金额
        $sparam['paymode'] = $param['paymode'];;
        $sparam['opentime'] = date('Y-m-d H:i:s');
        $sparam['bmp'] = $param['bmp'];
        $sparam['addo_popup'] = "应付:￥" . $param['yf_price'] . "\r\n" .
            "会员卡折扣:￥" . $param['dis_card_zk']/100 . "\r\n" .
            "会员卡等级折扣:￥" . $param['vip_card_zk']/100 . "\r\n" .
            "卡券折扣:￥" . $param['gel_card_zk']/100 . "\r\n" .
            "卡券折扣详情:" . $param['gel_info'] . "\r\n";
        $sparam['type'] = "micropay";
        $data['data'] = json_encode($sparam, JSON_UNESCAPED_UNICODE);
        $data['sign'] = $this->getSign($data, $keys);


        $result = $this->http_post($url, http_build_query($data));
        return $result;
    }

    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {

            if ($is_curlFile) {
                foreach ($param as $key => $val) {

                    if (substr($val, 0, 1) == '@') {

                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));

                    }
                }

            }
            $strPOST = $param;

        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);

        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    private function http_get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

//测试key acaeb16f692e71e513b3e15feda12340d235ea70
    private function getSign($data, $keys = "acaeb16f692e71e513b3e15feda12340d235ea70")
    {
        // print_r($data);
        foreach ($data as $key => $value) {
            if (isset($value) && !empty($value)) {
                $str[] = $key . "=" . $value;
            }
        }
        $datas = join("&", $str);
        // print_r($datas);
        $string = $datas . "&key=" . $keys;//acaeb16f692e71e513b3e15feda12340d235ea70
//        print_r($string);
        $sign = sha1($string);
//        print_r("字符串".$sign);
        return $sign;

    }

    /*key  duoerquanhuoqushebei20180610
     * appid  21
     *
     * */
    public function getparam($client_id, $device_id, $appid, $key)
    {

        $arr['client_id'] = $client_id;
        $arr['device_id'] = $device_id;
        $arr['appid'] = $appid;//"18";
        $arr['sign'] = $this->getSign2($arr, $key);
        $host = "http://dct.semoor.cn/o2o_api/client.php/{$arr['client_id']}/device/{$arr['device_id']}/get";
        $param = $this->http_post($host, http_build_query($arr));

        $param2 = json_decode($param, true);
        return $param2['secretkey'];

    }

    private function getSign2($data, $keys)
    {
        ksort($data);
        $str = [];
        foreach ($data as $key => $value) {
            if (isset($value) && !empty($value)) {
                $str[] = $key . "=" . $value;
            }
        }
        $datas = join("&", $str);
        $string = $datas . "&" . $keys;//"qydz20171208qydzpay";//


        $sign = sha1("/{$data['client_id']}/device/{$data['device_id']}/get?{$string}");//180951
        return $sign;
    }


}